package service

import (
	"fmt"
	"tads/server/app/business/models"
	"tads/server/app/business/service/dto"
	"tads/server/core/sdk/service"
)

type Icon struct {
	service.Service
}

func (i *Icon) Get(q *dto.IconGetRequests) (*dto.IconGetResponse, error) {
	var err error
	var data []models.Icon
	count := 0
	res := dto.IconGetResponse{}
	if q.PageIndex <= 0 || q.PageSize <= 0 {
		q.PageIndex = 1
		q.PageSize = 10
	}
	limit := q.PageSize
	offset := (q.PageIndex - 1) * q.PageSize

	if err = i.Postgres.Offset(offset).Where(" icon_name ILIKE ? or id = ?", fmt.Sprintf("%%%s%%", q.IconName), q.ID).Limit(limit).Find(&data).Error; err != nil {
		return res.NewPageResponse(count, q, data), err
	}
	count = len(data)
	if count == 0 {
		return res.NewPageResponse(count, q, data), nil
	}
	return res.NewPageResponse(count, q, data), nil
}

func (i *Icon) Inset(q *dto.IconInsetRequests, icon_path string, icon_attack_path string) error {
	var err error
	var data models.Icon
	q.Generate(&data, icon_path, icon_attack_path)
	if err = i.Postgres.Create(&data).Error; err != nil {
		return err
	}
	return nil
}

func (i *Icon) Update(q *dto.IconUpdateRequests, icon_path string, icon_attack_path string) error {
	var err error
	var data models.Icon
	q.Generate(&data, icon_path, icon_attack_path)
	if err = i.Postgres.Where(models.Icon{ID: q.ID}).Updates(&data).Error; err != nil {
		return err
	}
	return nil
}

func (i *Icon) Status(q *dto.IconStatusRequests) error {
	var err error
	var data models.Icon
	if err = i.Postgres.Model(&data).Where(models.Icon{ID: q.ID}).Updates(models.Icon{IsDeleted: q.Status}).Error; err != nil {
		return err
	}
	return nil
}
